package edu.neusoft.springhtmldemo.controller;

import edu.neusoft.springhtmldemo.domain.EchartsResult;
import edu.neusoft.springhtmldemo.service.EchartsResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class EchartsResultController {
    @Autowired
    private EchartsResultService service;
    @RequestMapping("testCharts")
    public List<EchartsResult> testCharts(Integer selectNum){
        List<EchartsResult> list=new ArrayList<>();

        for(int i=1;i<=selectNum;i++){
            List<Float> listd=new ArrayList<>();
            for(float j=0+i;j<7+i;j++){

                listd.add(j*100);
            }
            list.add(new EchartsResult("test"+i,"line",listd));
        }
//        for (TestResult testResult : list) {
//            System.out.println(testResult);
//        }
        return list;
    }

    @RequestMapping("TableQuery")
    public List<EchartsResult> TableQuery(String tableVal, String beginTimeVal, String endTimeVal, String[] chkArgVal){
//        System.out.println(tableVal+"-"+beginTimeVal+"-"+endTimeVal);
        String btime=beginTimeVal.substring(5,10);
        String etime=endTimeVal.substring(5,10);
        String timeName="";
        String[] argValues=new String[chkArgVal.length];
        String[] argumentsRobot={"实时功率","电池电压","剩余电量百分比","机器人运行速度","设备温度"};
        String[] argValuesRobot={"realTimePower","batteryVtage","batteryPct","robotSpd","robotTmpr"};
        String[] argumentsEnvironment={"温度","甲烷浓度","风速","氧气浓度","二氧化碳浓度","一氧化碳浓度"};
        String[] argValuesEnvironment={"envTmpr","methane","windSpd","O2","CO2","CO1"};
        String[] argumentsDevice={"电机温度","环境温度","轴承温度","输入端轴承震动","减速机输出"};
        String[] argValuesDevice={"motorTmpr","envTmpr","bearingTmpr","inputBearVbrte","reducerOutput"};
        if(tableVal.equals("robotworkinglogentity")){
            timeName="robotRecordTime";
            for(int i=0;i<chkArgVal.length;i++){
                for(int j=0;j<argValuesRobot.length;j++){
                    if(chkArgVal[i].equals(argValuesRobot[j])){
                        argValues[i]=argumentsRobot[j];
                        break;
                    }
                }
            }
        }else if(tableVal.equals("environmentlogentity")){
            timeName="envRecordTime";
            for(int i=0;i<chkArgVal.length;i++){
                for(int j=0;j<argValuesEnvironment.length;j++){
                    if(chkArgVal[i].equals(argValuesEnvironment[j])){
                        argValues[i]=argumentsEnvironment[j];
                        break;
                    }
                }
            }
        }else if(tableVal.equals("deviceworkinglogentity")){
            timeName="deviceRecodeTime";
            for(int i=0;i<chkArgVal.length;i++){
                for(int j=0;j<argValuesDevice.length;j++){
                    if(chkArgVal[i].equals(argValuesDevice[j])){
                        argValues[i]=argumentsDevice[j];
                        break;
                    }
                }
            }
        }

        List<EchartsResult> list=new ArrayList<>();
        for(int i=0;i<chkArgVal.length;i++){
//            System.out.println(chkArgVal[i]);
            String sql="select "+chkArgVal[i]+" from "+tableVal+" where DATE_FORMAT("+timeName+",'%m-%d')>= '"+btime+"' and DATE_FORMAT("+timeName+",'%m-%d') <= '"+etime+"'";
//            System.out.println(sql);
            List<Float> listf=service.queryEcharts(sql);
//            list.add(new EchartsResult(chkArgVal[i],"line",listf));
            list.add(new EchartsResult(argValues[i],"line",listf));
        }
        return list;
    }
}
